package org.leo.study.common.service.Impl;

import lombok.extern.slf4j.Slf4j;
import org.leo.study.common.config.SnowflakeIdConfig;
import org.leo.study.common.service.IdService;
import org.leo.study.common.utils.SnowflakeId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * Id生成服务实现
 * </p>
 *
 * @Package: org.leo.study.common.service.Impl
 * @ClassName: IdServiceImpl
 * @author: Li.Junlin
 * @date: 2019.10.12 15:37
 */
@Slf4j
@Service
public class IdServiceImpl implements IdService {

    private SnowflakeId snowflakeId;

    @Autowired
    private SnowflakeIdConfig snowflakeIdConfig;

    /**
     * snowflake算法生成Id
     *
     * @return SnowflakeId
     */
    @Override
    public Long snowflakeId() {
        if (snowflakeId == null) {
            synchronized (IdServiceImpl.class) {
                if (snowflakeId == null) {
                    snowflakeId = new SnowflakeId(snowflakeIdConfig.getDatacenterId(), snowflakeIdConfig.getMachineId());
                }
            }
        }
        return snowflakeId.nextId();
    }

    /**
     * md5算法生成Id
     *
     * @return md5Id
     */
    @Override
    public String Md5Id(Object o) {
        return null;
    }
}
